package org.iori.luodan.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.iori.json.JSONHelper;
import org.iori.luodan.base.BaseLogicFactory;
import org.iori.luodan.logic.DeviceLogic;
import org.iori.luodan.base.BaseServlet;

public class SetApnsInfoServlet extends BaseServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 744315239588347942L;
	
	private static DeviceLogic deviceLogic = BaseLogicFactory.getDeviceLogic();
    
	@Override
	protected String doExcute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		Object obj_device_token = request.getParameter("device_token");
		if (null == obj_device_token) {
			return null;
		}
		
		String deviceToken = String.valueOf(obj_device_token);
		if ("".equals(deviceToken)) {
			return null;
		}

		Object obj_userId = request.getParameter("user_id");
		int userId = -1;
		if (null != obj_userId) {
			userId = Integer.valueOf(String.valueOf(obj_userId).trim());
		}

		Boolean result = deviceLogic.insertOrReplaceDeviceInfo(deviceToken, userId);
		
		response.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		JSONHelper.writeObject(response, result);
		return null;
	}

}
